
package com.platform.modules.event.service.Impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.platform.common.utils.Query;
import com.platform.modules.event.dao.EventCategoryDao;
import com.platform.modules.event.entity.EventCategoryEntity;
import com.platform.modules.event.service.EventCategoryService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 活动和卡种关联表Service实现类
 *
 * @author suo
 * @date 2022-06-20 16:16:35
 */
@Service("eventCategoryService")
public class EventCategoryServiceImpl extends ServiceImpl<EventCategoryDao, EventCategoryEntity> implements EventCategoryService {

    @Override
    public List<EventCategoryEntity> queryAll(Map<String, Object> params) {
        return baseMapper.queryAll(params);
    }

    @Override
    public Page queryPage(Map<String, Object> params) {
        //排序
        params.put("sidx", "T.id");
        params.put("asc", false);
        Page<EventCategoryEntity> page = new Query<EventCategoryEntity>(params).getPage();
        return page.setRecords(baseMapper.selectEventCategoryPage(page, params));
    }

    @Override
    public boolean add(EventCategoryEntity eventCategory) {
        return this.save(eventCategory);
    }

    @Override
    public boolean update(EventCategoryEntity eventCategory) {
        return this.updateById(eventCategory);
    }

    @Override
    public boolean delete(String id) {
        return this.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(String[] ids) {
        return this.removeByIds(Arrays.asList(ids));
    }

    @Override
    public void saveOrUpdate(String id, List<String> categoryIdList) {
        Map<String, Object> map = new HashMap<>();
        map.put("event_id", id);
        //先删除活动与卡种关系
        this.removeByMap(map);

        if (categoryIdList == null || categoryIdList.size() == 0) {
            return;
        }

        //保存活动与卡种关系
        List<EventCategoryEntity> list = new ArrayList<>(categoryIdList.size());
        for (String categoryId : categoryIdList) {
            EventCategoryEntity eventCategoryEntity = new EventCategoryEntity();
            eventCategoryEntity.setEventId(id);
            eventCategoryEntity.setCategoryId(categoryId);

            list.add(eventCategoryEntity);
        }
        this.saveBatch(list);
    }

    @Override
    public List<String> queryCategoryList(String id){
        return baseMapper.queryCategoryList(id);
    }

    @Override
    public boolean deleteByEventIds(List<String> eventIds) {
        return baseMapper.deleteByEventId(eventIds)>0;
    }


}
